package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.BusCustomer;
import cn.wolfcode.car.business.mapper.BusCustomerMapper;
import cn.wolfcode.car.business.query.BusCustomerQuery;
import cn.wolfcode.car.business.service.IBusCustomerService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.List;

@Service
@Transactional
public class BusCustomerServiceImpl implements IBusCustomerService {

    @Autowired
    private BusCustomerMapper busCustomerMapper;

    @Override
    public TablePageInfo<BusCustomer> query(BusCustomerQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<BusCustomer>(busCustomerMapper.selectForList(qo));
    }


    @Override
    public void save(BusCustomer busCustomer) {

        busCustomerMapper.insert(busCustomer);
    }

    @Override
    public BusCustomer get(Long id) {
        return busCustomerMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(BusCustomer busCustomer) {
        BusCustomer bus = new BusCustomer();
        bus.setId(busCustomer.getId());
        bus.setName(busCustomer.getName());
        bus.setPhone(busCustomer.getPhone());
        bus.setBirthday(busCustomer.getBirthday());
        bus.setVip(busCustomer.getVip());
        bus.setRemark(busCustomer.getRemark());
        busCustomerMapper.updateByPrimaryKey(bus);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            busCustomerMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public Workbook exportExcel() {
        // 创建新的Excel 工作簿
        XSSFWorkbook workbook = new XSSFWorkbook();
        List<BusCustomer> busCustomers = busCustomerMapper.selectAll();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Sheet sheet = workbook.createSheet("服务明细");
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("客户姓名");
        row.createCell(1).setCellValue("联系方式");
        row.createCell(2).setCellValue("生日");
        row.createCell(3).setCellValue("是否vip");
        row.createCell(4).setCellValue("备注信息");
        for (int i = 0;i < busCustomers.size();i++){
            row = sheet.createRow(i + 1);
            BusCustomer customer = busCustomers.get(i);
            //row.createCell(0).setCellValue(i + 1);
            row.createCell(0).setCellValue(customer.getName());
            row.createCell(1).setCellValue(customer.getPhone());
            row.createCell(2).setCellValue(format.format(customer.getBirthday()));
      /*      if (customer.getVip() == 0 ){
                row.createCell(4).setCellValue("是");
            }else{
                row.createCell(4).setCellValue("否");
            }*/
            row.createCell(3).setCellValue(customer.getVip()==1?"是":"否");
            row.createCell(4).setCellValue(customer.getRemark());
        }
        return workbook;
    }
    @Override
    public void updateDeleteBatch(Long id) {

        busCustomerMapper.updateDeleteBatch(id);
    }

    @Override
    public List<BusCustomer> list() {
        return busCustomerMapper.selectAll();
    }

}

